package com.zto.sch.base.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadLearn3 {
    public static void main(String[] args) {
        //FutureTask包装我们的任务，FutureTask可以用于获取执行结果
        FutureTask<Integer> futureTask = new FutureTask(new MyCallable());
        //创建线程执行线程任务
        Thread thread = new Thread(futureTask);
        thread.start();

        try {
            //得到线程执行结果
            Integer integer = futureTask.get();
            System.out.println("得到线程处理结果：" + integer);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    //实现Callable接口，实现带返回值的任务
    static class MyCallable implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            int num = 0;
            for (int i = 1; i < 1000; i++) {
                System.out.println("输出打印:" + i);
                num += i;
            }
            return num;
        }
    }
}